package com.acme.bankapp.domain.bank;

import java.util.Set;
import java.util.TreeSet;

public class Client {
	private Set<Account> accounts = new TreeSet<>(new AccountComparator());
	private String name;
	private Gender gender;

	Client(String name, Gender gender, int... initialValueAndOverDraft)
			throws IllegalArgumentException {
		this.name = name;
		this.gender = gender;

		if (initialValueAndOverDraft.length == 1) {
			Account a = new SavingsAccount(initialValueAndOverDraft[0]);
			accounts.add(a);
		} else if (initialValueAndOverDraft.length == 2) {
			Account a = new CheckingAccount(initialValueAndOverDraft[0],
					initialValueAndOverDraft[1]);
			accounts.add(a);
		} else {
			throw new IllegalArgumentException(
					"One or two arguments are needed!");
		}

	}

	public String getName() {
		return name;
	}

	public String getGender() {
		return gender.getString();
	}

	public Set<Account> getAccounts() {
		return accounts;
	}
	
	public void setAccounts(Set<Account> accounts) {
		this.accounts=accounts;
	}

	public void getClientSalutation() {
		System.out.print(gender.getString() + name);
	}

	public boolean equals(Object object) {
		if (this == object) {
			return true;
		}
		if (object == null) {
			return false;
		}
		if (this.getClass() != object.getClass()) {
			return false;
		}
		Client ob = (Client) object;
		if (this.getName() == ob.getName()
				&& this.getGender() == ob.getGender()) {
			return true;
		} else {
			return false;
		}
	}

	public int hashCode() {
		int code = 11;
		int k = 7;
		code = k * code + name.hashCode();
		code = k * code + gender.hashCode();
		return code;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer("Client: name -- ");
		sb.append(name);
		sb.append(", gender -- ");
		sb.append(gender);
		sb.append("\nAccounts: ");
		for (Account ac : accounts) {
			sb.append(ac.toString());
		}
		return sb.toString();
	}
}
